﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KaoWuYouLibs.Dal;
using KaoWuYouLibs.Model;
using System.IO;
using System.Data.OleDb;

namespace KaoWuYouLibs.Bll
{
    public class TestPaperManagementBusiness
    {
        Test_QuestionAccess test_QuestionAccess = new Test_QuestionAccess();
        TestPaperAccess testPaperAccess = new TestPaperAccess();
        QuestionsAccess questionAccess = new QuestionsAccess();

        public bool[] ChangeQuestionID(int[] newQuestionIDs,int[] oldQuestionIDs, int TestPaperID)
        {
            bool[] changeQuestionResults = new bool[oldQuestionIDs.Length];

            for (int i = 0; i < oldQuestionIDs.Length; i++)
            {
                try
                {
                    //changeQuestionResults[i] = test_QuestionAccess.DeleteQuestionID(oldQuestionIDs[i], TestPaperID) 
                    //    && test_QuestionAccess.AddQuestionID(newQuestionIDs[i], TestPaperID);
                }
                catch (Exception ex)
                {
                    changeQuestionResults[i] = false;
                }
            }
            return changeQuestionResults;
        }

        public bool[] DeleteQuestionID(int[] deleteQuestionIDs, int TestPaperID)
        {
            bool[] deleteQuestionResults = new bool[deleteQuestionIDs.Length];

            for (int i = 0; i < deleteQuestionIDs.Length; i++)
            {
                try
                {
                    deleteQuestionResults[i] = test_QuestionAccess.DeleteQuestionID(deleteQuestionIDs[i], TestPaperID);
                }
                catch (Exception ex)
                {
                    deleteQuestionResults[i] = false;
                }
            }
            return deleteQuestionResults;
        }

        public List<TestPaperModel> GetTestPaperListBySubjectIDAndSubclassID(int SubjectID, int SubclassID)
        {
            try
            {
                List<TestPaperModel> re = testPaperAccess.FindTestPapersBySubjectIDAndSubclassID(SubjectID, SubclassID);
                foreach (var testPaper in re)
                {
                    testPaper.Questions = questionAccess.FindOwnedQuestions(testPaper.TestPaperID);
                    testPaper.TotalMark = 0;
                    foreach (var q in testPaper.Questions)
                    {
                        testPaper.TotalMark += q.Mark;
                    }
                }
                return re;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public TestPaperModel GetTestPaperDetails(int TestPaperID)
        {
            try
            {
                TestPaperModel re = testPaperAccess.FindTestPaper(TestPaperID);
                re.Questions = questionAccess.FindOwnedQuestions(TestPaperID);
                re.TotalMark = 0;
                foreach (var question in re.Questions)
                {
                    re.TotalMark += question.Mark;
                    if (question.Genre.Equals("选择题"))
                    {
                        string[] strs = question.Stem.Split('$');
                        question.Stem = strs[0];
                        question.Options = new List<string>();
                        question.Options.Add(strs[1]);
                        question.Options.Add(strs[2]);
                        question.Options.Add(strs[3]);
                        question.Options.Add(strs[4]);
                    }
                }

                return re;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool ImportTestPaper(string filePath)
        {
            try
            {
                string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
                OleDbConnection conn = new OleDbConnection(connStr);
                string sqlCmd = "SELECT * FROM [TestPaper$]";
                conn.Open();
                OleDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sqlCmd;
                OleDbDataReader odr = cmd.ExecuteReader();

                int testPaperID = 0;
                int questionID = 0;
                int quantity = 0;

                if (odr.Read())
                {
                    string testPaperName = odr[0] as string;
                    quantity = Convert.ToInt32(odr[1]);
                    int subjectID = Convert.ToInt32(odr[2]);
                    int subclassID = Convert.ToInt32(odr[3]);
                    testPaperID = testPaperAccess.AddTestPaper(testPaperName, subjectID, subclassID);
                }

                while (odr.Read())
                {
                    string stem = odr[0] as string;
                    string answer = odr[1] as string;
                    int subjectID = Convert.ToInt32(odr[2]);
                    int subclassID = Convert.ToInt32(odr[3]);
                    string genre = odr[4] as string;
                    int score = Convert.ToInt32(odr[5]);

                    questionID = questionAccess.AddQuestion(stem, answer, subjectID, subclassID, genre);
                    test_QuestionAccess.AddQuestionID(questionID, testPaperID, score);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
